package com.example.perplonews.a_d_fragment.e_mine

import BaseFragment
import android.annotation.SuppressLint
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.request.RequestOptions
import com.example.perplonews.R
import com.example.perplonews.a_a_mvp.contract.UserContract
import com.example.perplonews.a_a_mvp.model.models.UserModel
import com.example.perplonews.a_a_mvp.presenter.presenters.UserPresenter
import com.example.perplonews.a_b_entity.UserItemEntity
import com.example.perplonews.a_b_entity.UserMenuEntity
import com.example.perplonews.a_b_entity.greendao.UserEntity
import com.example.perplonews.a_b_utils.LjlUtils
import com.example.perplonews.a_d_fragment.e_mine.adapter.UserAdapter
import com.example.perplonews.a_d_fragment.e_mine.adapter.UserItemAdapter


class MineFragment : BaseFragment<UserPresenter>(), UserContract.UserVIew {
    private val mineRec: RecyclerView by lazy { findViewById<RecyclerView>(R.id.mine_rec) }

    private var adapter: UserAdapter? = null
    private var meunadapter: UserItemAdapter? = null
    var mineimg: ImageView? = null
    private var minetext: TextView? = null
    private var bro: MyBro? = null

    override fun findlayout(): Int {
        return R.layout.fragment_mine
    }

    override fun initView() {

    }

    override fun initData() {
        mypre = UserPresenter(UserModel(), this)
        mypre.iniitemtData()

    }

    override fun startPage(bundle: Bundle, clazz: Class<Any>) {

    }
    //设置页面数据
    @SuppressLint("InflateParams")
    override fun refreshMenu(list: ArrayList<UserMenuEntity>, list2: ArrayList<UserItemEntity>) {
        val inflate = layoutInflater.inflate(R.layout.card_user_1, null)
        minetext = inflate.findViewById<TextView>(R.id.card_user_login)
        var card_qiandao = inflate.findViewById<TextView>(R.id.card_user_qiandao)
        val card_recycle = inflate.findViewById<RecyclerView>(R.id.card_user_recycle)
        mineimg = inflate.findViewById<ImageView>(R.id.mine_head_img)


        //设置适配器  添加头部剧
        val inflate1 = layoutInflater.inflate(R.layout.card_user_2, null)
        //文字的点击  判断进到哪里
        minetext?.setOnClickListener {

            val boolean = activity?.getSharedPreferences("log", Context.MODE_PRIVATE)
                ?.getBoolean("islog", false)
            if (boolean!!) {
                val userName = activity?.getSharedPreferences("username", Context.MODE_PRIVATE)
                    ?.getString("name", null)
                val userImg = activity?.getSharedPreferences("userimg", Context.MODE_PRIVATE)
                    ?.getString("img", null)
                val intent = Intent(activity, UserActivity::class.java)
                intent.putExtra("username",userName )
                intent.putExtra("userimg", userImg)
                startActivity(intent)
            } else {
                startActivity(Intent(activity, Login_Activity::class.java))
            }

        }


        meunadapter = UserItemAdapter(list)
        card_recycle.adapter = meunadapter
        val gridLayoutManager = GridLayoutManager(context, 4)
        gridLayoutManager.orientation = GridLayoutManager.VERTICAL
        card_recycle.layoutManager = gridLayoutManager




        adapter = UserAdapter(list2)



        mineRec.adapter = adapter
        mineRec.layoutManager = LinearLayoutManager(context)
        adapter?.addHeaderView(inflate)
        adapter?.addHeaderView(layoutInflater.inflate(R.layout.fffview, null))
        adapter?.addHeaderView(inflate1)

    }

//设置头像和姓名
    @SuppressLint("ApplySharedPref", "SetTextI18n")
    fun refresUser(userdata: UserEntity) {
        val userImg = userdata.getUser_img()
        val userName = userdata.getUser_name()
        Glide.with(mineimg?.context).load("https://i01piccdn.sogoucdn.com/082fb9a40bab4521").apply(
            RequestOptions.bitmapTransform(CircleCrop())).into(mineimg)

        minetext?.setText("用户：" + userName)

        activity?.getSharedPreferences("log", Context.MODE_PRIVATE)
            ?.edit()?.putBoolean("islog", true)?.commit()

        activity?.getSharedPreferences("username", Context.MODE_PRIVATE)?.edit()
            ?.putString("name", userName)?.commit()
        activity?.getSharedPreferences("userimg", Context.MODE_PRIVATE)?.edit()
            ?.putString("img", userImg)?.commit()
        activity?.getSharedPreferences("userid", Context.MODE_PRIVATE)?.edit()
            ?.putString("id", userdata.user_id.toString())?.commit()




    }
    //activity 创建时
    @SuppressLint("SetTextI18n")
    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        bro = MyBro()
        //注册广播
        val intentfilter = IntentFilter()
        intentfilter.addAction("islog")
        activity?.registerReceiver(bro, intentfilter)


        //判断是否登录
        val boolean =
            activity?.getSharedPreferences("log", Context.MODE_PRIVATE)?.getBoolean("islog", false)
        if (boolean!!) {
            val string = activity?.getSharedPreferences("username", Context.MODE_PRIVATE)
                ?.getString("name", null)
            val img = activity?.getSharedPreferences("userimg", Context.MODE_PRIVATE)
                ?.getString("img", null)


            Glide.with(mineimg?.context).load("https://i01piccdn.sogoucdn.com/082fb9a40bab4521").into(mineimg)
            minetext?.setText("用户：" + string)

        } else {
            return
        }




    }

    override fun onDestroy() {
        super.onDestroy()
        activity?.unregisterReceiver(bro)
    }


    //广播接受者
    inner class MyBro() : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {

            if (intent.action.equals("islog")) {
                val userdata = intent.getParcelableExtra<UserEntity>("userdata")
                refresUser(userdata!!)
            }
            if (intent.action.equals("unlog")) {
                //取消登录了
                minetext?.setText("登录/注册")
                Glide.with(mineimg?.context).load(R.mipmap.res_7icon).into(mineimg)

            }
        }
    }

}





